package com.foreveross.bsl.mam.app.application.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

import javax.inject.Named;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;

import com.foreveross.bsl.common.utils.bean.BeanHelper;
import com.foreveross.bsl.common.utils.enums.OperatEnum;
import com.foreveross.bsl.mam.app.application.SettingCategoryApplication;
import com.foreveross.bsl.mam.app.application.vo.SettingCategoryVo;
import com.foreveross.bsl.mam.model.app.domain.SettingCategory;

/**
 * 此类描述的是：系统设置接口实现业务逻辑类
 * 
 * @author: panhm@foreveross.com
 * @version: 2013-7-24 上午10:13:36
 */

@Named("settingCategoryApplication")
public class SettingCategoryApplicationImpl implements
		SettingCategoryApplication {

	@Override
	public SettingCategoryVo saveEntity(SettingCategoryVo entity) {
		// TODO Auto-generated method stub
		SettingCategory setting = new SettingCategory();
		BeanHelper.copyProperties(setting, entity);
		setting = (SettingCategory) setting.save();
		BeanHelper.copyProperties(entity, setting);
		return entity;
	}

	@Override
	public SettingCategoryVo updateEntity(SettingCategoryVo entity) {
		// TODO Auto-generated method stub
		SettingCategory setting = new SettingCategory();
		BeanHelper.copyProperties(setting, entity);
		setting = (SettingCategory) setting.save();
		BeanHelper.copyProperties(entity, setting);
		return entity;
	}

	@Override
	public void deleteEntity(String id) {
		// TODO Auto-generated method stub
		SettingCategory setting = new SettingCategory();
		setting = (SettingCategory) setting.findOne(id);
		setting.setAvailable("0");
		setting.setModified_time(new Date());
		setting.save();
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<SettingCategoryVo> findByType(String type, String available) {
		// TODO Auto-generated method stub
		List<SettingCategoryVo> result = new ArrayList<SettingCategoryVo>();
		SettingCategory setting = new SettingCategory();
		List<SettingCategory> list = setting.findListByProperty(new String[] {
				"type", "available" }, new OperatEnum[] { OperatEnum.EQ,
				OperatEnum.EQ }, new String[] { type, available });
		if (list != null) {
			SettingCategoryVo vo;
			for (SettingCategory sett : list) {
				vo = new SettingCategoryVo();
				BeanHelper.copyProperties(vo, sett);
				result.add(vo);
			}
		}
		return result;
	}

	@Override
	public SettingCategoryVo getEntity(String id) {
		SettingCategory setting = new SettingCategory();
		SettingCategoryVo vo = new SettingCategoryVo();
		setting = (SettingCategory) setting.findOne(id);
		BeanHelper.copyProperties(vo, setting);
		return vo;
	}

	@SuppressWarnings("unchecked")
	@Override
	public SettingCategoryVo checkName(String name, String type) {
		// TODO Auto-generated method stub
		SettingCategory setting = new SettingCategory();
		List<SettingCategory> list = setting.findListByProperty(new String[] {
				"name", "type", "available" }, new OperatEnum[] {
				OperatEnum.EQ, OperatEnum.EQ, OperatEnum.EQ }, new String[] {
				name, type, "1" });
		if (list != null && list.size() > 0) {
			SettingCategory sett = list.get(0);
			SettingCategoryVo vo = new SettingCategoryVo();
			BeanHelper.copyProperties(vo, sett);
			return vo;
		}
		return null;
	}

	@SuppressWarnings("rawtypes")
	@Override
	public Page<SettingCategoryVo> findByPageable(String type, Pageable pageable) {
		// TODO Auto-generated method stub
		SettingCategory setting = new SettingCategory();
		Page page = setting.findByProperty(
				new String[] { "type", "available" }, new OperatEnum[] {
						OperatEnum.EQ, OperatEnum.EQ }, new String[] { type,
						"1" }, pageable);
		List<SettingCategoryVo> result = new ArrayList<SettingCategoryVo>();

		if (page != null && page.getContent() != null) {
			SettingCategoryVo vo;
			for (Iterator iterator = page.getContent().iterator(); iterator
					.hasNext();) {
				SettingCategory obj = (SettingCategory) iterator.next();
				vo = new SettingCategoryVo();
				BeanHelper.copyProperties(vo, obj);
				result.add(vo);

			}
			Page<SettingCategoryVo> pageVO = new PageImpl<SettingCategoryVo>(
					result, pageable, page.getTotalElements());
			return pageVO;
		}
		return null;
	}

}
